package cn.yj.core.geo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *
 * </p>
 *
 * @author 永健
 * @since 2019-07-30 18:20
 */
@Component
public class GeoHandler implements Geo
{

    @Autowired
    GeoRequest request;


    @Value("${applications.geoKey}")
    private String geoKey;

    /**
     * 将其它的坐标转换成高德坐标
     *
     * @param coordons 坐标类型
     * @param locations 坐标
     * @return
     * @throws Exception
     */
    @Override
    public String coordinateConvert(Cordons coordons, String locations) throws Exception
    {
        Map<String, Object> map = new HashMap<>(3);
        map.put("key", geoKey);
        map.put("coordsys", coordons.name());
        map.put("locations", locations);
        Map<String, Object> res = request.httpGet(GEO_COVERT_URL, map);
        return res.get("locations").toString();
    }

    /**
     * 根据坐标获取详细信息
     * @param location 高德坐标
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> coordToAddress(String location) throws Exception
    {
        Map<String, Object> map = new HashMap<>(3);
        map.put("key", geoKey);
        map.put("location", location);
        map.put("radius",1000);
        map.put("extensions","all");
        map.put("roadlevel",0);
        return request.httpGet(GEO_COORDI_TO_ADDRESSS_URL, map);
    }

    @Override
    public Map<String, Object> district() throws Exception
    {
        Map<String, Object> param = new HashMap<>(2);
        param.put("extensions", "base");
        param.put("subdistrict", "2");
        param.put("key", geoKey);
        Map<String, Object> map = request.httpGet(GEO_DISTRICT_URL, param);
        return map;
    }

    /**
     * @param lngLat
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getProAndCityByngLat(String lngLat) throws Exception
    {
        Map<String, Object> param = new HashMap<>(2);
        param.put("radius", 1000);
        param.put("location", lngLat);
        param.put("extensions", "base");
        param.put("batch", "false");
        param.put("key", geoKey);
        return request.httpGet(GEO_GET_PRO_CITY_URL, param);
    }

    /**
     * @param address
     * @param city
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> psToLocation(String address, String city) throws Exception
    {
        Map<String, Object> param = new HashMap<>(2);
        param.put("address", address);
        param.put("city", city);
        param.put("key", geoKey);
        return request.httpGet(GEO_PS_TO_LOCTION, param);
    }



    @Override
    public Map<String, Object> getMsgByAddress(String address) throws Exception
    {
        Map<String, Object> param = new HashMap<>(2);
        param.put("address", address);
        param.put("key", geoKey);
        return request.httpGet(GEO_MSG_URL, param);
    }


    @Override
    public String[] getCityByLocation(String location) throws Exception {
        Map<String, Object> param = new HashMap<>(2);
        param.put("location", location);
        param.put("key", geoKey);
        Map<String, Object> regeocode = (Map<String, Object>) request.httpGet(GEO_COORDI_TO_ADDRESSS_URL, param).get(
                "regeocode");
        Object o =  regeocode.get(
                "addressComponent");
        Map<String, Object> map = (Map<String, Object>) o;
        return new String[]{map.get("province").toString(),map.get("city").toString(),regeocode.get("formatted_address").toString()};
    }
}
